#!/usr/bin/python

import wx
import sys

if '..' not in sys.path: sys.path.insert(0,'..') #?? remove once in main source tree

from base import *
import fh

class FindReplaceDialog(wx.Dialog):
	
	FIND,REPLACE_NEXT,REPLACE_ALL=range(3)
	
	def __init__(me,parent,id=wx.ID_ANY, use_metal=True, **kw):
		wx.Dialog.__init__(me,parent,id,'Find & Replace',**kw)

		# This extra style can be set after the UI object has been created.
		if 'wxMac' in wx.PlatformInfo and use_metal:
			me.SetExtraStyle(wx.DIALOG_EX_METAL)

		top_box=wx.BoxSizer(wx.HORIZONTAL)
		
		if 1:
			find_box=wx.BoxSizer(wx.VERTICAL)
			top_box.Add(find_box,1,wx.RIGHT,5)
			
			if 1:
				find_box_row1=wx.BoxSizer(wx.HORIZONTAL)
				find_box.Add(find_box_row1,1,wx.EXPAND|wx.BOTTOM,8)
				
				c=wx.CheckBox(me,wx.ID_ANY,'Match Case')
				find_box_row1.Add(c,0,wx.RIGHT,5)
				me.match_case=c
					
				me.find_text=wx.TextCtrl(me,wx.ID_ANY,"<search string>",size=(200,-1))
				find_box_row1.Add(me.find_text,1,wx.EXPAND)
				me.find_text.SetFocus()
				me.find_text.SetSelection(-1,-1)
			
			if 1:
				find_box_row2=wx.BoxSizer(wx.HORIZONTAL)
				find_box.Add(find_box_row2,1,wx.EXPAND)

				c=wx.CheckBox(me,wx.ID_ANY,'Whole Word')
				find_box_row2.Add(c,1,wx.RIGHT,5)
				me.whole_word=c
				
				c=wx.CheckBox(me,wx.ID_ANY,'Reg Exp')
				find_box_row2.Add(c,1)
				me.reg_exp=c
				
				b=wx.Button(me,wx.ID_FIND,"Find Next")
				b.SetDefault()
				me.Bind(wx.EVT_BUTTON,me.OnFind,b)
				find_box_row2.Add(b,1,wx.RIGHT,5)
				
				b=wx.Button(me,wx.ID_CLOSE,"Close")
				#me.Bind(wx.EVT_BUTTON, me.OnCloseClicked,b)
				find_box_row2.Add(b,1)
		if 1:
			replace_box=wx.BoxSizer(wx.VERTICAL)
			top_box.Add(replace_box,1)
			
			if 1:
				me.replace_text=wx.TextCtrl(me,wx.ID_ANY,"<replacement string>",size=(200,-1))
				replace_box.Add(me.replace_text,1,wx.EXPAND|wx.BOTTOM,8)
				me.replace_text.SetSelection(-1,-1)
				
			if 1:
				replace_row2=wx.BoxSizer(wx.HORIZONTAL)
				replace_box.Add(replace_row2,1,wx.ALIGN_RIGHT)
				
				replace_row2.Add(wx.StaticText(me,wx.ID_ANY,"Replace:"),0,wx.RIGHT,5)
				
				b=wx.Button(me,wx.ID_ANY,"Next")
				# ?? b.SetHelpText("Replace and find again")
				#me.Bind(wx.EVT_BUTTON, me.OnFindClicked,b)
				replace_row2.Add(b,1,wx.RIGHT,5)
				
				b=wx.Button(me,wx.ID_ANY,"Skip")
				#me.Bind(wx.EVT_BUTTON, me.OnFindClicked,b)
				replace_row2.Add(b,1,wx.RIGHT,5)
				
				b=wx.Button(me,wx.ID_ANY,"All")
				#me.Bind(wx.EVT_BUTTON, me.OnCloseClicked,b)
				replace_row2.Add(b,1)
			
		
		me.SetSizer(top_box)
		top_box.Fit(me)
		
		me._events_=fh.EventSet('find')
		
	def _common_event_struct(me):
		return struct(src=me,
			whole_word=me.whole_word.GetValue(),
			match_case=me.match_case.GetValue(),
			reg_exp=me.reg_exp.GetValue(),
			find_string=me.find_text.GetValue(),
			replacement_string=me.replace_text.GetValue())
		
	def OnFind(me,e):
		e=me._common_event_struct()
		e.type=me.FIND
		me._events_.find.broadcast(e)
		#me.EndModal(0)


class MainPanel(wx.Panel):

	def __init__(me,parent):
		wx.Panel.__init__(me,parent)		
		me.button = wx.Button(me,wx.ID_ANY,"Button", (25, 50))
		me.Bind(wx.EVT_BUTTON, me.OnClick, me.button)
		
	def OnClick(me,e):
		d = FindReplaceDialog(me)
		d.Show(True)
		d.Centre()

		
class MainFrame(wx.Frame):

	def __init__(me):
			
		wx.Frame.__init__(me,parent=None)
		MainPanel(parent=me)
		

def on_find(e):
	print e
	
app = wx.PySimpleApp()
d = FindReplaceDialog(None)
d._events_.find.broadcast( on_find )
d.ShowModal()
d.Centre()
app.MainLoop()